package com.example.neusoft.yuejian.Fragment;

import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.neusoft.yuejian.Base.BaseFragment;
import com.example.neusoft.yuejian.Base.BaseListener;
import com.example.neusoft.yuejian.Base.BaseModel;


import com.example.neusoft.yuejian.Bean.ShtuBean;
import com.example.neusoft.yuejian.Bean.data;

import com.example.neusoft.yuejian.R;
import com.squareup.picasso.Picasso;
import java.util.List;

import retrofit2.Call;
    public class ShaituFragment extends BaseFragment implements View.OnClickListener {
        private ImageView xj;
        private List<data> lists;
        private RecyclerView st;


        @Override
        public int getLayoutFile() {
            return R.layout.fragment_shaitu;
        }

        @Override
        public void initView() {
            st =  view.findViewById(R.id.st);
            st.setLayoutManager(new LinearLayoutManager(this.getActivity()));
            xj=view.findViewById(R.id.xj);

        }

        @Override
        public void initEvent() {
          xj.setOnClickListener(this);
        }

        @Override
        public void initData() {
            getShaitudata();

        }


        private void getShaitudata() {
            BaseModel<ShtuBean> model = new BaseModel<>();
            Call<ShtuBean> call = model.service.shaituList();
            model.callEnqueue(call, list);
        }



        BaseListener<ShtuBean> list=new BaseListener<ShtuBean>() {
            @Override
            public void onResponse(ShtuBean shtuBean) {
                lists=shtuBean.getData();
                st.setAdapter(new ShaituAdapter(lists));

            }

            @Override
            public void onFail(String msg) {
                showToast(msg);
            }
        };


        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.xj:
                    Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    //startActivityForResult(intent);
                    startActivity(intent);
                    //onActivityResult(intent);
            }
        }


        public class ShaituAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


            private List<data> list;

            public ShaituAdapter( List<data> list){

                this.list = list;


            }
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                LayoutInflater inflater=LayoutInflater.from(getActivity());
                View v = inflater.inflate(R.layout.st_item, parent, false);
                return new ViewHolder(v);
            }


            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                data d= list.get(position) ;
                if (null == d)
                    return;
                com.example.neusoft.yuejian.Fragment.ShaituFragment.ShaituAdapter.ViewHolder viewHolder =
                        (com.example.neusoft.yuejian.Fragment.ShaituFragment.ShaituAdapter.ViewHolder) holder;
                viewHolder.bt.setText(d.getSmessage());
                viewHolder.zz.setText(d.getUname());
                viewHolder.fbsj.setText(d.getSreleasetime());
                viewHolder.pls.setText(d.getNum());;
                Picasso.with(getActivity()).load("http://172.19.181.175/YueJian/" +
                        "public/static/img/shaitu/"+d.getSpic()).into(viewHolder.st_pic);        }

            @Override
            public int getItemCount() {
                return list.size();
            }

            private class ViewHolder extends RecyclerView.ViewHolder {
                ImageView st_pic;
                TextView bt,zz,fbsj,pls;
                public ViewHolder(View v) {
                    super(v);
                    st_pic =  v.findViewById(R.id.st_pic);
                    bt =  v.findViewById(R.id.bt);
                    zz =  v.findViewById(R.id.zz);
                    fbsj =  v.findViewById(R.id.fbsj);
                    pls =  v.findViewById(R.id.pls);
                }
            }
        }
}
